﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;


namespace WuxiWTO.Web.backend
{
    public partial class imageNewsEdit : BasePage
    {
        WTODataContext contex = new WTODataContext();
        int cid = 0;
        /// <summary>
        /// 更新模式
        /// </summary>
        protected bool UpdateMode
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    cid = int.Parse(Request.QueryString["id"]);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindGrid();
                if (UpdateMode)
                {
                    GetInfo();
                }
            }
        }

        private void GetInfo()
        {
            image_new cat = contex.image_news.SingleOrDefault(p => p.ID == cid);
            if (cat != null)
            {
                this.txtTitle.Text = cat.news_title;
                this.editor1.Text = cat.news_content;
                if (!string.IsNullOrEmpty(cat.image_path))
                {
                    this.litPic.Text = string.Format("<a target='_blank' href=\"{0}\">查看图片</a>", "../" + cat.image_path);
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "fa", "alert('参数传递失败');", true);
            }
        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            image_new link;
            if (UpdateMode)
            {
                link = contex.image_news.SingleOrDefault(p => p.ID == cid);
                if (link == null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "fa1", "alert('参数传递失败');", true);
                    return;
                }
            }
            else
                link = new image_new();
            link.news_title = this.txtTitle.Text.Trim();
            link.news_content = this.editor1.Text;
            if (!UpdateMode)
            {
                link.created_date = DateTime.Now;
            }
            link.update_date = DateTime.Now;

            if (fileLink.HasFile)
            {
                if (".jpg.jpeg.gif.png".Contains(System.IO.Path.GetExtension(fileLink.FileName).ToLower()))
                {
                    string filePath = Server.MapPath(@"~/uploads/news/");
                    string fileName = fileLink.FileName;
                    string savePath = filePath + fileName;
                    if (!System.IO.Directory.Exists(filePath))
                    {
                        System.IO.DirectoryInfo path = System.IO.Directory.CreateDirectory(filePath);
                    }
                    fileLink.SaveAs(savePath);

                    Utility.MakeThumbnail(savePath, filePath + "thumb_" + fileName, Utility.ThumbType.CUT, 242, 182);
                    link.image_path= "uploads/news/" + "thumb_" + fileName;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "fa1", "alert('上传图片格式错误,请上传后缀名为:.jpg/.jpeg/.gif/.png格式的图片');", true);
                    return;
                }
            }
            if (!UpdateMode)
            {
                contex.image_news.InsertOnSubmit(link);
            }

            contex.SubmitChanges();
            Response.Redirect("imageNewsList.aspx");
        }
    }
}